package gentle.controller;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.BaseController;
import gentle.entity.PaymentRecordInfo;
import gentle.entity.SearchPaymentRecordInfo;
import gentle.factory.PageFactory;
import gentle.service.IPaymentRecordService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;


/**
 *结账明细
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/paymentRecord")
public class PaymentRecordController extends BaseController{
    @Autowired
    IPaymentRecordService paymentRecordService;

    /**
     * 结账明细列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object rechargeList(SearchPaymentRecordInfo info) {
        if(info.getAbbreviation() == null || info.getAbbreviation() =="" || info.getAbbreviation() =="null"){
            return null;
        }else{
            Page<PaymentRecordInfo> page = new PageFactory<PaymentRecordInfo>().defaultPage();
            try {
                info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                List<PaymentRecordInfo> list = paymentRecordService.selectPageListInfo(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = paymentRecordService.selectPageListInfo(page, info);
                }
                page.setTotal(paymentRecordService.getQueryPagesCount(page, info));
                page.setRecords(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    @RequestMapping("/sumBorrowAmount")
    @ResponseBody
    public HashMap sumBorrowAmount(@RequestParam(required = false) String checkoutMethod,@RequestParam(required = false) String reach,
                                   @RequestParam(required = false) String fieldTc,@RequestParam(required = false) String doctor,
                                   @RequestParam(required = false) String startTime,@RequestParam(required = false) String customerStatus,
                                   @RequestParam(required = false) String endTime,@RequestParam(required = false) String abbreviation) {
        return paymentRecordService.sumBorrowAmount(checkoutMethod,reach,fieldTc,doctor,startTime,customerStatus,endTime,abbreviation);
    }

    @RequestMapping(value = "/updateCheckoutMethod", method = RequestMethod.POST)
    @ResponseBody
    public int updateCheckoutMethod(@RequestBody PaymentRecordInfo paymentRecordInfo) {
        return paymentRecordService.updateCheckoutMethod(paymentRecordInfo);
    }

    @RequestMapping(value = "/paymentRecordExport")
    public void reservationsExport(@Valid SearchPaymentRecordInfo searchInfo, HttpServletResponse response) {
        paymentRecordService.reservationsExport(searchInfo,response);
    }

}